﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Dao;
using Core.Entity;
using System.Data;
namespace Core.Business
{
    public class AccountBus
    {
        private AccountDao dao;
        public AccountBus() {
            dao = new AccountDao();
        }

        public bool insert(Account account) {
            return dao.insert(account);
        }

        public bool update(Account account) {
            return dao.update(account);
        }

        public bool delete(int id) {
            return dao.delete(id);
        }

        public bool delete(String ids) {
            return dao.delete(ids);
        }

        public Account get(int id) {
            DataTable dt = dao.get(id);
            Account account = new Account();

            if (dt.Rows.Count > 0)
            {
                account.Id = Convert.ToInt32(dt.Rows[0]["ACC_ID"]);
                account.CountryId = Convert.ToInt32(dt.Rows[0]["CNT_ID"]);
                account.BankId = Convert.ToInt32(dt.Rows[0]["BNK_ID"]);
                account.UserId = Convert.ToInt32(dt.Rows[0]["USR_ID"]);
                account.AccountTypeId = Convert.ToInt32(dt.Rows[0]["ACT_ID"]);
                account.CityId = Convert.ToInt32(dt.Rows[0]["CTY_ID"]);
                account.Code = dt.Rows[0]["ACC_CODE"].ToString();
                account.OwnerName = dt.Rows[0]["ACC_OWNER_NAME"].ToString();
                account.DelegateName = dt.Rows[0]["ACC_DELEGATE_NAME"].ToString();
                account.Address = dt.Rows[0]["ACC_ADDRESS"].ToString();
                account.AlternateEmail = dt.Rows[0]["ACC_ALT_EMAIL"].ToString();
                account.CellPhone = dt.Rows[0]["ACC_CELL_PHONE"].ToString();
                account.HomePhone = dt.Rows[0]["ACC_HOME_PHONE"].ToString();
                account.FaxNumber = dt.Rows[0]["ACC_FAX_NUMBER"].ToString();
                account.TaxCode = dt.Rows[0]["ACC_TAX_CODE"].ToString();
                account.IdNumber = dt.Rows[0]["ACC_ID_NUMBER"].ToString();
                account.BirthDay = Convert.ToDateTime(dt.Rows[0]["ACC_BIRTHDAY"]);
                account.HomePage = dt.Rows[0]["ACC_HOME_PAGE"].ToString();
                account.BankAccountNumber = dt.Rows[0]["ACC_BANK_ACCOUNT_NUMBER"].ToString();
                account.IdFrontSide = dt.Rows[0]["ACC_ID_FRONT_SIDE"].ToString();
                account.IdBackSide = dt.Rows[0]["ACC_ID_BACK_SIDE"].ToString();

            }
            return account;
        }

        public List<Account> getAll() {
            DataTable dt = dao.getAll();
            List<Account> list = new List<Account>();

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Account account = new Account();
                    account.Id = Convert.ToInt32(dr["ACC_ID"]);
                    account.CountryId = Convert.ToInt32(dr["CNT_ID"]);
                    account.BankId = Convert.ToInt32(dr["BNK_ID"]);
                    account.UserId = Convert.ToInt32(dr["USR_ID"]);
                    account.AccountTypeId = Convert.ToInt32(dr["ACT_ID"]);
                    account.CityId = Convert.ToInt32(dr["CTY_ID"]);
                    account.Code = dr["ACC_CODE"].ToString();
                    account.OwnerName = dr["ACC_OWNER_NAME"].ToString();
                    account.DelegateName = dr["ACC_DELEGATE_NAME"].ToString();
                    account.Address = dr["ACC_ADDRESS"].ToString();
                    account.AlternateEmail = dr["ACC_ALT_EMAIL"].ToString();
                    account.CellPhone = dr["ACC_CELL_PHONE"].ToString();
                    account.HomePhone = dr["ACC_HOME_PHONE"].ToString();
                    account.FaxNumber = dr["ACC_FAX_NUMBER"].ToString();
                    account.TaxCode = dr["ACC_TAX_CODE"].ToString();
                    account.IdNumber = dr["ACC_ID_NUMBER"].ToString();
                    account.BirthDay = Convert.ToDateTime(dr["ACC_BIRTHDAY"]);
                    account.HomePage = dr["ACC_HOME_PAGE"].ToString();
                    account.BankAccountNumber = dr["ACC_BANK_ACCOUNT_NUMBER"].ToString();
                    account.IdFrontSide = dr["ACC_ID_FRONT_SIDE"].ToString();
                    account.IdBackSide = dr["ACC_ID_BACK_SIDE"].ToString();

                    list.Add(account);
                }

            }
            return list;
        }

        public Account getAccountByUserId(int userId)
        {
            DataTable dt = dao.getAccountByUserId(userId);
            Account account = new Account();

            if (dt.Rows.Count > 0)
            {
                account.Id = Convert.ToInt32(dt.Rows[0]["ACC_ID"]);
                account.CountryId = Convert.ToInt32(dt.Rows[0]["CNT_ID"]);
                account.BankId = Convert.ToInt32(dt.Rows[0]["BNK_ID"]);
                account.UserId = Convert.ToInt32(dt.Rows[0]["USR_ID"]);
                account.AccountTypeId = Convert.ToInt32(dt.Rows[0]["ACT_ID"]);
                account.CityId = Convert.ToInt32(dt.Rows[0]["CTY_ID"]);
                account.Code = dt.Rows[0]["ACC_CODE"].ToString();
                account.OwnerName = dt.Rows[0]["ACC_OWNER_NAME"].ToString();
                account.DelegateName = dt.Rows[0]["ACC_DELEGATE_NAME"].ToString();
                account.Address = dt.Rows[0]["ACC_ADDRESS"].ToString();
                account.AlternateEmail = dt.Rows[0]["ACC_ALT_EMAIL"].ToString();
                account.CellPhone = dt.Rows[0]["ACC_CELL_PHONE"].ToString();
                account.HomePhone = dt.Rows[0]["ACC_HOME_PHONE"].ToString();
                account.FaxNumber = dt.Rows[0]["ACC_FAX_NUMBER"].ToString();
                account.TaxCode = dt.Rows[0]["ACC_TAX_CODE"].ToString();
                account.IdNumber = dt.Rows[0]["ACC_ID_NUMBER"].ToString();
                account.BirthDay = Convert.ToDateTime(dt.Rows[0]["ACC_BIRTHDAY"]);
                account.HomePage = dt.Rows[0]["ACC_HOME_PAGE"].ToString();
                account.BankAccountNumber = dt.Rows[0]["ACC_BANK_ACCOUNT_NUMBER"].ToString();
                account.IdFrontSide = dt.Rows[0]["ACC_ID_FRONT_SIDE"].ToString();
                account.IdBackSide = dt.Rows[0]["ACC_ID_BACK_SIDE"].ToString();

            }
            return account;
        }

    }
}
